import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

class CommonPicker {
  static Future<int?>? showPicker(
    BuildContext context, {
    List<String>? options,
    int? value = 0,
    double height = 300,
  }) {
    FixedExtentScrollController controller =
        FixedExtentScrollController(initialItem: value ?? 0);
    return showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) {
        return Container(
          color: Colors.grey[200],
          height: height,
          child: Column(children: [
            SizedBox(
              height: 40,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: const Text('取消')),
                  TextButton(
                      onPressed: () {
                        Navigator.of(context).pop(controller.selectedItem);
                      },
                      child: const Text('确定'))
                ],
              ),
            ),
            Expanded(
                child: CupertinoPicker(
              scrollController: controller,
              backgroundColor: Theme.of(context).scaffoldBackgroundColor,
              itemExtent: 32, // item 高度
              onSelectedItemChanged: (int value) {},
              children: options!.map((e) => Text(e)).toList(),
            ))
          ]),
        );
      },
    );
  }
}
